﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Jy123.DBUtility;
using Jy123.BLL.Public;

namespace Jy123.BLL.Club
{
    public class CModule : Jy123.BLL.Public.Module<Jy123.Model.Club_Main>
    {
        /// <summary>
        /// 组件升级(条件)
        /// </summary>
        /// <param name="id">组件ID</param>
        /// <param name="ckey">条件组</param>
        /// <param name="cvalue">条件值组</param>
        /// <param name="money">升级所需金钱</param>
        /// <returns>成功失败</returns>
        public override bool LevelUpByCondition(int id, List<string> cKey, List<int> cValue, decimal money)
        {
            Jy123.BLL.Club_Main BLLClubMain = new Jy123.BLL.Club_Main();
            Jy123.Model.Club_Main club = BLLClubMain.GetModel(id);
            Jy123.BLL.User_Main BLLUserMain = new Jy123.BLL.User_Main();
            Jy123.Model.User_Main user = BLLUserMain.GetModel(club.FUser.Value);
            if (user.Cash >= money)
            {
                StringBuilder strSql = new StringBuilder();
                strSql.Append("SELECT ");
                int count = cKey.Count;
                for (int i = 0; i < count - 1; i++)
                {
                    strSql.Append(cKey[i].ToString());
                    strSql.Append(",");
                }
                strSql.Append(cKey[count - 1]);
                strSql.AppendFormat(" FROM Club_Main WHERE ID = {0}", id);
                DataSet dsResult = DbHelperSQL.Query(strSql.ToString());//获取组件对应字段的值
                if (dsResult != null)
                {
                    DataRow drResult = dsResult.Tables[0].Rows[0];
                    for (int j = 0; j < cValue.Count; j++)
                    {
                        string keyName = cKey[j].ToString();
                        int iResult = int.Parse(drResult[keyName].ToString());
                        int iValue = int.Parse(cValue[j].ToString());
                        if (iResult < iValue)
                        {
                            return false;//比较组件对应各字段是否达到升级条件
                        }
                    }
                }
                else
                {
                    return false;
                }
                return true;
            }
            return false;
        }

        /// <summary>
        /// 升级俱乐部，扣除相应费用
        /// </summary>
        /// <param name="venues">俱乐部</param>
        /// <param name="money">升级费用</param>
        /// <param name="user">用户</param>
        /// <returns>是否成功</returns>
        public bool UpgradeModule(Jy123.Model.Club_Main club, decimal money, Jy123.Model.User_Main user)
        {
            try
            {
                Club_Main BLLClub = new Club_Main();
                User_Main BLLUser = new User_Main();
                Public_Level BLLLevel = new Public_Level();
                Model.Public_Level cLevel = BLLLevel.GetModel(club.FLevel.Value);
                int nLNumber = cLevel.LevelNumber.Value + 1;
                string strWhere = string.Format("FJType = {0} AND LevelNumber = {1}", club.FJType.Value, nLNumber.ToString());
                Model.Public_Level nLevel = BLLLevel.GetModelList(strWhere)[0];
                club.FLevel = nLevel.ID;
                BLLClub.Update(club);
                user.Cash = Convert.ToInt32(user.Cash.Value - money);
                BLLUser.Update(user);
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 返回好友某个类型俱乐部组件列表
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <param name="JType">俱乐部类型</param>
        /// <returns>该类型俱乐部对象组</returns>
        public List<Jy123.Model.Club_Main> FriendsClub(int userId, int JType, bool status)
        {
            Club_Main BLLClubMain = new Club_Main();
            StringBuilder strWhere = new StringBuilder();
            strWhere.AppendFormat("FJType = {0} ", JType);
            strWhere.Append("AND FUser IN (");
            strWhere.Append("SELECT FFriend FROM User_Friend ");
            strWhere.AppendFormat("WHERE FUser = {0}) ", userId);
            if (status) {
                strWhere.Append("AND ID IN (");
                strWhere.AppendFormat("SELECT ID FROM Module_Main WHERE MStatus = 0 OR  MStatus = 2)");//表示组件为空闲或者比赛
            }
            List<Jy123.Model.Club_Main> resultList = BLLClubMain.GetModelList(strWhere.ToString());
            return resultList;
        }

        /// <summary>
        /// 返回好友某个类型俱乐部组件列表
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <param name="JType">俱乐部类型</param>
        /// <returns>该类型俱乐部对象组</returns>
        public List<Jy123.Model.Club_Main> FriendsEmptyClub(int userId, int JType, bool status)
        {
            Club_Main BLLClubMain = new Club_Main();
            StringBuilder strWhere = new StringBuilder();
            strWhere.AppendFormat("FJType = {0} ", JType);
            strWhere.Append("AND FUser IN (");
            strWhere.Append("SELECT FFriend FROM User_Friend ");
            strWhere.AppendFormat("WHERE FUser = {0}) ", userId);
            if (status)
            {
                strWhere.Append("AND ID IN (");
                strWhere.AppendFormat("SELECT ID FROM Module_Main WHERE MStatus = 0)");//表示组件为空闲或者比赛
            }
            List<Jy123.Model.Club_Main> resultList = BLLClubMain.GetModelList(strWhere.ToString());
            return resultList;
        }

        /// <summary>
        /// 返回好友同类型俱乐部组件列表
        /// </summary>
        /// <param name="id">组件ID</param>
        /// <returns>该类型组件对象组</returns>
        public override List<Jy123.Model.Club_Main> FriendsModule(int id)
        {
            Jy123.BLL.Club_Main BLLClubMain = new Jy123.BLL.Club_Main();
            Jy123.Model.Club_Main club = BLLClubMain.GetModel(id);
            int userId = club.FUser.Value;
            StringBuilder strWhere = new StringBuilder();
            strWhere.AppendFormat("FJType = {0} ", club.FJType);
            strWhere.Append("AND FUser IN (");
            strWhere.Append("SELECT FFriend FROM User_Friend ");
            strWhere.AppendFormat("WHERE FUser = {0})", userId);
            List<Jy123.Model.Club_Main> resultList = BLLClubMain.GetModelList(strWhere.ToString());
            return resultList;
        }
        /// <summary>
        /// 返回组件列表
        /// </summary>
        /// <param name="field">列名</param>
        /// <param name="count">总数</param>
        /// <param name="order">排序类型([T]asc[F]Desc)</param>
        /// <returns>该类型组件对象组</returns>
        public override List<Jy123.Model.Club_Main> OrderModule(string field, int count, bool order)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.AppendFormat("SELECT TOP {0} * FROM Club_Main ", count);
            string strOrder = "ASC";
            if (!order)
            {
                strOrder = "DESC";
            }
            strSql.AppendFormat(" ORDER BY {0} {1}", field, strOrder);
            DataSet ds = DbHelperSQL.Query(strSql.ToString());//获取DataSet
            List<Jy123.Model.Club_Main> modelList = DataSetToModelList(ds);
            return modelList;
        }
        /// <summary>
        /// 返回好友组件列表
        /// </summary>
        /// <param name="id">组件ID</param>
        /// <param name="field">列名</param>
        /// <param name="count">总数</param>
        /// <param name="order">排序类型([T]asc[F]Desc)</param>
        /// <returns>该类型组件对象组</returns>
        public override List<Jy123.Model.Club_Main> OrderMFriend(int id, string field, int count, bool order)
        {
            Jy123.BLL.Club_Main BLLClubMain = new Jy123.BLL.Club_Main();
            Jy123.Model.Club_Main club = BLLClubMain.GetModel(id);
            int userId = club.FUser.Value;
            StringBuilder strSql = new StringBuilder();
            strSql.AppendFormat("SELECT TOP {0} * FROM Club_Main ", count);
            strSql.AppendFormat("WHERE FJType = {0} ", club.FJType);
            strSql.Append("AND FUser IN (");
            strSql.Append("SELECT FFriend FROM User_Friend ");
            strSql.AppendFormat("WHERE FUser = {0})", userId);
            string strOrder = "ASC";
            if (!order)
            {
                strOrder = "DESC";
            }
            strSql.AppendFormat(" ORDER BY {0} {1}", field, strOrder);
            DataSet ds = DbHelperSQL.Query(strSql.ToString());//获取DataSet

            List<Jy123.Model.Club_Main> modelList = DataSetToModelList(ds);
            return modelList;
        }
        /// <summary>
        /// 分页返回好友俱乐部
        /// </summary>
        /// <param name="userId">当前用户ID</param>
        /// <param name="sportType">当前组件运动类型</param>
        /// <param name="currentpage">当前页</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="field">排序字段</param>
        /// <param name="order">排序</param>
        /// <returns>好友俱乐部列表</returns>
        public List<Jy123.Model.Club_Main> PaginationMFriend(int userId, int sportType, int currentpage, int pageSize, string field, bool order)
        {
            Jy123.Model.MPagination mPagination = new Jy123.Model.MPagination();
            StringBuilder strFilter = new StringBuilder();
            strFilter.AppendFormat("FUser IN (SELECT FFriend FROM User_Friend WHERE FUser = {0})", userId);
            strFilter.AppendFormat("AND FJType IN (SELECT FJType FROM Public_JType WHERE FSportType = {0}) ", sportType);
            string strOrder = " ASC";
            if (!order)
            {
                strOrder = " DESC";
            }
            mPagination.Tables = "Club_Main";
            mPagination.PrimaryKey = "ID";
            mPagination.Fields = "*";
            mPagination.CurrentPage = currentpage;
            mPagination.Filter = strFilter.ToString();
            mPagination.PageSize = pageSize;
            mPagination.Sort = field + strOrder;
            mPagination.Group = string.Empty;
            DataSet ds = Pagination.GetList(mPagination);
            List<Jy123.Model.Club_Main> modelList = DataSetToModelList(ds);
            return modelList;
        }
        /// <summary>
        /// 将DataSet转换成Club_Main的List
        /// </summary>
        /// <param name="ds">传入的DataSet</param>
        /// <returns>Club_Main的List</returns>
        private List<Jy123.Model.Club_Main> DataSetToModelList(DataSet ds)
        {
            List<Jy123.Model.Club_Main> modelList = new List<Jy123.Model.Club_Main>();//转换成List
            int rowsCount = ds.Tables[0].Rows.Count;
            if (rowsCount > 0)
            {
                Jy123.Model.Club_Main model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new Jy123.Model.Club_Main();
                    if (ds.Tables[0].Rows[n]["ID"].ToString() != "")
                    {
                        model.ID = int.Parse(ds.Tables[0].Rows[n]["ID"].ToString());
                    }
                    model.CName = ds.Tables[0].Rows[n]["CName"].ToString();
                    model.Logo = ds.Tables[0].Rows[n]["Logo"].ToString();
                    if (ds.Tables[0].Rows[n]["CStatus"].ToString() != "")
                    {
                        model.CStatus = int.Parse(ds.Tables[0].Rows[n]["CStatus"].ToString());
                    }
                    if (ds.Tables[0].Rows[n]["FUser"].ToString() != "")
                    {
                        model.FUser = int.Parse(ds.Tables[0].Rows[n]["FUser"].ToString());
                    }
                    if (ds.Tables[0].Rows[n]["FJType"].ToString() != "")
                    {
                        model.FJType = int.Parse(ds.Tables[0].Rows[n]["FJType"].ToString());
                    }
                    if (ds.Tables[0].Rows[n]["FLevel"].ToString() != "")
                    {
                        model.FLevel = int.Parse(ds.Tables[0].Rows[n]["FLevel"].ToString());
                    }
                    if (ds.Tables[0].Rows[n]["FColor"].ToString() != "")
                    {
                        model.FColor = int.Parse(ds.Tables[0].Rows[n]["FColor"].ToString());
                    }
                    if (ds.Tables[0].Rows[n]["FLine"].ToString() != "")
                    {
                        model.FLine = int.Parse(ds.Tables[0].Rows[n]["FLine"].ToString());
                    }
                    if (ds.Tables[0].Rows[n]["Supporter"].ToString() != "")
                    {
                        model.Supporter = int.Parse(ds.Tables[0].Rows[n]["Supporter"].ToString());
                    }
                    if (ds.Tables[0].Rows[n]["Skill"].ToString() != "")
                    {
                        model.Skill = int.Parse(ds.Tables[0].Rows[n]["Skill"].ToString());
                    }
                    if (ds.Tables[0].Rows[n]["PhysicalFitness"].ToString() != "")
                    {
                        model.PhysicalFitness = int.Parse(ds.Tables[0].Rows[n]["PhysicalFitness"].ToString());
                    }
                    if (ds.Tables[0].Rows[n]["LiveNess"].ToString() != "")
                    {
                        model.LiveNess = int.Parse(ds.Tables[0].Rows[n]["LiveNess"].ToString());
                    }
                    if (ds.Tables[0].Rows[n]["Morale"].ToString() != "")
                    {
                        model.Morale = int.Parse(ds.Tables[0].Rows[n]["Morale"].ToString());
                    }
                    if (ds.Tables[0].Rows[n]["Experience"].ToString() != "")
                    {
                        model.Experience = int.Parse(ds.Tables[0].Rows[n]["Experience"].ToString());
                    }
                    if (ds.Tables[0].Rows[n]["Star"].ToString() != "")
                    {
                        if ((ds.Tables[0].Rows[n]["Star"].ToString() == "1") || (ds.Tables[0].Rows[n]["Star"].ToString().ToLower() == "true"))
                        {
                            model.Star = true;
                        }
                        else
                        {
                            model.Star = false;
                        }
                    }
                    if (ds.Tables[0].Rows[n]["Coach"].ToString() != "")
                    {
                        if ((ds.Tables[0].Rows[n]["Coach"].ToString() == "1") || (ds.Tables[0].Rows[n]["Coach"].ToString().ToLower() == "true"))
                        {
                            model.Coach = true;
                        }
                        else
                        {
                            model.Coach = false;
                        }
                    }
                    if (ds.Tables[0].Rows[n]["Equipment"].ToString() != "")
                    {
                        if ((ds.Tables[0].Rows[n]["Equipment"].ToString() == "1") || (ds.Tables[0].Rows[n]["Equipment"].ToString().ToLower() == "true"))
                        {
                            model.Equipment = true;
                        }
                        else
                        {
                            model.Equipment = false;
                        }
                    }
                    model.Description = ds.Tables[0].Rows[n]["Description"].ToString();
                    if (ds.Tables[0].Rows[n]["Assets"].ToString() != "")
                    {
                        model.Assets = decimal.Parse(ds.Tables[0].Rows[n]["Assets"].ToString());
                    }
                    if (ds.Tables[0].Rows[n]["WinTimes"].ToString() != "")
                    {
                        model.WinTimes = int.Parse(ds.Tables[0].Rows[n]["WinTimes"].ToString());
                    }
                    if (ds.Tables[0].Rows[n]["LoseTimes"].ToString() != "")
                    {
                        model.LoseTimes = int.Parse(ds.Tables[0].Rows[n]["LoseTimes"].ToString());
                    }
                    modelList.Add(model);
                }
            }
            return modelList;
        }
        /// <summary>
        /// 获取俱乐部所有球员
        /// </summary>
        /// <param name="clubId">俱乐部ID</param>
        /// <returns>俱乐部所有球员列表</returns>
        public List<Jy123.Model.Club_StarRecord> GetAllClubSportsMan(int clubId)
        {
            StringBuilder strWhere = new StringBuilder();
            int star = Convert.ToInt32(Jy123.BLL.Public.EnumConfig.AthleteType.Star);
            int sportsman = Convert.ToInt32(Jy123.BLL.Public.EnumConfig.AthleteType.Sportsman);
            strWhere.AppendFormat("FClub = {0} AND (SType = {1} OR SType={2})", clubId, star, sportsman);
            Club_StarRecord BLLStarRecord = new Club_StarRecord();
            List<Jy123.Model.Club_StarRecord> listResult = BLLStarRecord.GetModelList(strWhere.ToString());
            return listResult;
        }
        /// <summary>
        /// 获取俱乐部所有普通球员
        /// </summary>
        /// <param name="clubId">俱乐部ID</param>
        /// <returns>俱乐部所有普通球员列表</returns>
        public List<Jy123.Model.Club_StarRecord> GetClubSportsMan(int clubId)
        {
            StringBuilder strWhere = new StringBuilder();
            int sportsman = Convert.ToInt32(Jy123.BLL.Public.EnumConfig.AthleteType.Sportsman);
            strWhere.AppendFormat("FClub = {0} AND SType = {1} ", clubId, sportsman);
            Club_StarRecord BLLStarRecord = new Club_StarRecord();
            List<Jy123.Model.Club_StarRecord> listResult = BLLStarRecord.GetModelList(strWhere.ToString());
            return listResult;
        }
        /// <summary>
        /// 获取俱乐部所有球星
        /// </summary>
        /// <param name="clubId">俱乐部ID</param>
        /// <returns>俱乐部所有球星列表</returns>
        public List<Jy123.Model.Club_StarRecord> GetClubStar(int clubId)
        {
            StringBuilder strWhere = new StringBuilder();
            int star = Convert.ToInt32(Jy123.BLL.Public.EnumConfig.AthleteType.Star);
            strWhere.AppendFormat("FClub = {0} AND SType = {1}", clubId, star);
            Club_StarRecord BLLStarRecord = new Club_StarRecord();
            List<Jy123.Model.Club_StarRecord> listResult = BLLStarRecord.GetModelList(strWhere.ToString());
            return listResult;
        }
        /// <summary>
        /// 获取俱乐部所有教练
        /// </summary>
        /// <param name="clubId">俱乐部ID</param>
        /// <returns>俱乐部所有教练列表</returns>
        public List<Jy123.Model.Club_StarRecord> GetClubCoche(int clubId)
        {
            StringBuilder strWhere = new StringBuilder();
            int coche = Convert.ToInt32(Jy123.BLL.Public.EnumConfig.AthleteType.Coche);
            strWhere.AppendFormat("FClub = {0} AND SType = {1}", clubId, coche);
            Club_StarRecord BLLStarRecord = new Club_StarRecord();
            List<Jy123.Model.Club_StarRecord> listResult = BLLStarRecord.GetModelList(strWhere.ToString());
            return listResult;
        }


    }
}
